package com.intellij.licensecommon.net.impl;

import com.intellij.licensecommon.f.Protocol;
import com.intellij.licensecommon.f.b;
import com.intellij.licensecommon.message.Message;
import com.intellij.licensecommon.net.*;
import com.intellij.licensecommon.net.d;
import com.intellij.licensecommon.net.f;
import org.jetbrains.annotations.NotNull;

public class c implements Network {
    private boolean running;
    private final NetworkSocketFactory networkSocketFactory;
    private final AddressFactory addressFactory;
    private final Protocol protocol;
    private NetworkSocket networkSocket;
    private Address address;
    private NetworkTarget networkTarget;
    private g h;
    private g i;
    private NetworkSocket j;
    private Address k;
    private j l;

    public c(@NotNull NetworkSocketFactory paramNetworkSocketFactory, @NotNull AddressFactory paramAddressFactory, @NotNull Protocol paramProtocol) {
        this.networkSocketFactory = paramNetworkSocketFactory;
        this.addressFactory = paramAddressFactory;
        this.protocol = paramProtocol;
    }

    public void a(NetworkTarget paramNetworkTarget)
            throws f, com.intellij.licensecommon.net.c, d {
        if (this.running) {
            throw new f();
        }
        this.networkTarget = paramNetworkTarget;
        this.networkSocket = this.networkSocketFactory.a();
        this.k = this.networkSocket.e();
        this.address = this.addressFactory.a();
        this.j = this.networkSocketFactory.b(this.address);
        this.l = new j();
        this.l.start();
        this.h = new g(this, this.networkSocket);
        this.h.start();
        this.i = new g(this, this.j);
        this.i.start();
        try {
            this.networkSocket.c();
            this.j.c();
        } catch (InterruptedException localInterruptedException) {
            throw new d(localInterruptedException);
        }
        this.running = true;
    }

    public void b() {
        if (this.networkSocket != null) {
            this.networkSocket.b();
        }
        if (this.j != null) {
            this.j.b();
        }
        try {
            if (this.h != null) {
                this.h.join();
            }
            if (this.i != null) {
                this.i.join();
            }
        } catch (InterruptedException localInterruptedException) {
            localInterruptedException.printStackTrace();
        }
        if (this.l != null) {
            this.l.a();
        }
        this.running = false;
    }

    public void a(Message paramMessage, Address paramAddress)
            throws com.intellij.licensecommon.net.g {
        try {
            byte[] arrayOfByte = this.protocol.a(paramMessage);
            this.networkSocket.a(new i(arrayOfByte, paramAddress));
        } catch (com.intellij.licensecommon.f.c localc) {
            throw new com.intellij.licensecommon.net.g(localc);
        }
    }

    public boolean isRunning() {
        return this.running;
    }

    public void a(i parami) {
        Address localAddress = parami.a();
        if (a(localAddress)) {
            return;
        }
        Message localMessage;
        try {
            localMessage = this.protocol.a(parami.b());
        } catch (b localb) {
            return;
        }
        this.l.a(this.networkTarget, localMessage, localAddress);
    }

    private boolean a(Address paramAddress) {
        return paramAddress.equals(this.k);
    }

    public void a(Throwable paramThrowable) {
        this.networkTarget.a(paramThrowable);
    }

    public void a(Message paramMessage)
            throws com.intellij.licensecommon.net.g {
        a(paramMessage, this.address);
    }
}

